package Server.ServerMainFrame.ServerMainFrameModel;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.*;

import java.io.*;

import Packet.*;

import java.util.*;
import Core.Model.*;

import javax.swing.*;

import Server.ServerMainFrame.ServerMainFrameModel.ServerToTeamThread.*;

import Server.ServerMainFrame.ServerMainFrameModel.*;

public class InitializeServerSocket extends Thread
{
    public InitializeServerSocket(ServerMainFrameModel model)
    {
    	this.serverSocket = model.getServerSocket();
    	this.model = model;
    }
    
    public void run()
    {
    	initializePortAndSocket();
    	waitConnection();
    }
    
    public void initializePortAndSocket()
    {
    	try
    	{
    		BufferedReader in = new BufferedReader(new FileReader(this.serverPortConfigureFile));
    		this.serverPort = new Integer(in.readLine());
    	}
    	catch(FileNotFoundException exception)
    	{
    		System.out.println("FielNotFoundException happened when initializeServerSocket");
    		exception.printStackTrace();
    	}
    	catch(IOException exception)
    	{
    	    System.out.println("IOException happened when initializeServerSocket");	
    	    exception.printStackTrace();
    	}
    	try
    	{
    	    this.serverSocket = new ServerSocket(this.serverPort);
    	}
    	catch(IOException exception)
    	{
    	    System.out.println("IOException happened when initializeServerSocket");	
    	}
    }
    
    public void waitConnection()
    {
    	System.out.println("port " + this.serverPort);
    	while (true)
    	{
    		try
    		{
    		    Socket connectSocket = this.serverSocket.accept();
    		    
    		    System.out.println("server : team connected!");
    		    
    		    readyToStartServerThread(connectSocket);
    		}
    		catch(IOException exception)
    		{
    			System.out.println("IOException happened when server wait for connection");
    			exception.printStackTrace();
    		}
    	}
    }
    
    
    public void readyToStartServerThread(Socket connectSocket)
    {
        try
        {
        	System.out.println("here0");
        	
        	System.out.println(connectSocket);
        	
        	ObjectInputStream objectInputStream = new ObjectInputStream(connectSocket.getInputStream());
        	
        	System.out.println("here1");
        	
        	Packet packet = (Packet)objectInputStream.readObject();
        	
        	objectInputStream.close();
        	
        	System.out.println("here2");
        	
        	Properties prop = (Properties)packet.getContent();
        	
        	JOptionPane.showConfirmDialog(null, "message");
        	
        	System.out.println("Server receive Packet : " + prop.get(PacketFactory.CLIENT_USER_TYPE));
        	
        	if (prop.get(PacketFactory.CLIENT_USER_TYPE).equals(ClientType.TEAM))
        	{
        		ServerToTeamThread thread = new ServerToTeamThread(connectSocket, this.model);
        		thread.start();
        		thread.dealPacket(packet);
        	}
        }
        catch(IOException exception)
        {
            System.out.println("IOException");
            exception.printStackTrace();
        }
        catch(ClassNotFoundException exception)
        {
        	System.out.println("ClassNotFoundException exception");
        	exception.printStackTrace();
        }
    }
    
    private ServerMainFrameModel model;
    
    private ServerSocket serverSocket;
    private int serverPort;
    private String serverPortConfigureFile = new String("c:\\server_port.txt");
}
